package com.atguigu.eduteacher.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduteacher.javabean.EduCourse;
import com.atguigu.eduteacher.javabean.dto.CourseInfoDto;
import com.atguigu.eduteacher.javabean.dto.CoursePublishDto;
import com.atguigu.eduteacher.javabean.dto.CourseQuery;
import com.atguigu.eduteacher.service.IEduCourseService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-21
 */
@RestController
@RequestMapping("/eduservice/edu-course")
@CrossOrigin
@RequiredArgsConstructor
public class EduCourseController {


    private final IEduCourseService courseService;

    @ApiOperation(value = "新增课程")
    @PostMapping
    public R saveCourseInfo(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required =
                    true)
            @RequestBody CourseInfoDto courseInfoDto) {
        String courseId = courseService.saveCourseInfo(courseInfoDto);
        if (!StringUtils.isEmpty(courseId)) {
            return R.ok().data("courseId", courseId);
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("{id}")
    public R getById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        CourseInfoDto courseInfo = courseService.getCourseInfoById(id);
        return R.ok().data("item", courseInfo);
    }


    @ApiOperation(value = "更新课程")
    @PutMapping("{id}")
    public R updateCourseInfoById(
            @ApiParam(name = "CourseInfoForm", value = "课程基本信息", required =
                    true)
            @RequestBody CourseInfoDto courseInfoDto,
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        courseService.updateCourseInfoById(courseInfoDto);
        return R.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("publish-info/{id}")
    public R getCoursePublishVoById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        CoursePublishDto courseInfoForm =
                courseService.getCoursePublishDtoById(id);
        return R.ok().data("item", courseInfoForm);
    }


    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public R publishCourseById(
            @ApiParam(name = "id", value = "课程ID", required = true)
            @PathVariable String id) {
        boolean result = courseService.publishCourseById(id);
        return R.ok().data("result", result);
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public R pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            CourseQuery courseQuery) {
        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

}
